package com.xsr.controller;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project springMvcday03
 * @date 2022/9/13 11:11
 */
@Controller
@RequestMapping("/upload")
@ResponseBody
public class UploadController {
    @RequestMapping("/test01")
    public String test1(MultipartFile multipartFile, HttpServletRequest request ) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        String s = UUID.randomUUID().toString();
        String extension = FilenameUtils.getExtension(originalFilename);
        s=s+"."+extension;
        System.out.println(s);
        String fileName="D:\\qfStudy\\picture\\"+s;
        request.getSession().setAttribute("fileName",s);
        multipartFile.transferTo(new File(fileName));
          return "OK";
    }
    @RequestMapping("test02")
    public  void  download(String fileName, HttpServletResponse response ) throws IOException {
        String fileName1="D:\\qfStudy\\picture\\"+fileName;
        FileInputStream fileInputStream = new FileInputStream(new File(fileName1));
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(fileInputStream,outputStream);
    }
}
